<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {
	
	private $idSedeActual;
	private $nameSedeActual;
	private $limit = 3;

	// Constructor de Clase
	function __construct() {
		parent::__construct();

		$this->load->library('usuarioLib');
		$this->load->library('homelib');

		$this->load->model('Model_Sede');
		$this->load->model('Model_Aviso');
		$this->load->model('Model_Actividad');
				
		$this->form_validation->set_message('required', 'Debe ingresar un valor para %s');
		$this->form_validation->set_message('loginok', 'Usuario o Clave incorrectos');
		$this->form_validation->set_message('matches', '%s no coincide con %s');
		$this->form_validation->set_message('cambiook', 'No se puede realizar el cambio de clave');
		$this->idSedeActual = null;
		$this->nameSedeActual = null;
	}

	public function index() {
		$data['contenido'] = 'home/index';
		$data['titulo'] = 'Inicio';
		$data['sede_name'] = '';
		$this->load->view('templateHome', $data);
	}

	public function acerca_de() {
		$data['sede_name'] = null;
		$data['contenido'] = 'home/acerca_de';
		$data['titulo'] = 'Acerca De';
		$this->load->view('templateHome', $data);
	}

	public function acceso_denegado() {
		$data['sede_name'] = null;
		$data['contenido'] = 'home/acceso_denegado';
		$data['titulo'] = 'Denegado';
		$this->load->view('templateHome', $data);
	}

	public function ingreso() {
		$data['sede_name'] = null;
		$data['contenido'] = 'home/ingreso';
		$data['titulo'] = 'Ingreso';
		$this->load->view('templateHome', $data);
	}

	public function ingresar() {
		$this->form_validation->set_rules('login', 'Usuario', 'required|callback_loginok');
		$this->form_validation->set_rules('password', 'Clave', 'required');
		if($this->form_validation->run() == FALSE) {
			$this->ingreso();
		}
		else {
			$this->homelib->redireccionar();		
		}
	}

	public function loginok() {
		$login = $this->input->post('login');
		$password = $this->input->post('password');
		return $this->usuariolib->login($login, md5($password));
	}

	public function salir() {
		$this->session->sess_destroy();
		redirect('home/index');
	}

	public function cambio_clave() {
		$param = $this->uri->segment(3);
		$data['contenido'] = 'home/cambio_clave';
		$data['titulo'] = 'Cambiar Clave';
		$data['id'] = $param;
		$this->load->view('template', $data);
	}

	public function cambiar_clave() {
		$this->form_validation->set_rules('clave_act', 'Clave Actual', 'required|callback_cambiook');
		$this->form_validation->set_rules('clave_new', 'Clave Nueva', 'required|matches[clave_rep]');
		$this->form_validation->set_rules('clave_rep', 'Repita Nueva', 'required');
		if($this->form_validation->run() == FALSE) {
			$this->cambio_clave();
		}
		else {
			$param = $this->uri->segment(3);
			if ($param != null){
				redirect('usuario/index');
			}
			else{		
				redirect('home/index');
			}
		}
	}

	public function cambiook() {
		$act = $this->input->post('clave_act');
		$new = $this->input->post('clave_new');
		return $this->usuariolib->cambiarPWD(md5($act), md5($new));
	}
	
	/*
	 * 
	 * en las sedes usamos un template diferente
	 * templateDefault
	 * en este templa cada vista para las distintas sedes se generara
	 * 
	 * */
	
	private function paramSede(){
		$uri_segment = 3;
		$sedeId = $this->uri->segment($uri_segment);
		return $sedeId;
	}

	private function nameSede(){
		$sedeId = $this->paramSede();
		$query = $this->Model_Sede->find($sedeId);

		return $query->name;
	}	
	
	public function sede() {
		$nameSede = $this->nameSede();
	/*
		$data['queryAvisos'] = $this->Model_Sede->find($sedeId);
		$data['queryActividades'] = $this->Model_Sede->find($sedeId);
		$data['queryPDI'] = $this->Model_Sede->find($sedeId);						
		//$data['queryExamenes'] = $this->Model_Sede->find($sedeId);
		*/	
		$data['contenido'] = 'home/sede';
		$data['titulo'] = 'Sede';
		$data['sede_name'] = $nameSede;
		$this->load->view('templateDefault', $data);
	}
	
	private function getAvisos(){
		$sedeId = $this->paramSede();
		$param = 'sede_id = '.$sedeId;
		$query = $this->Model_Aviso->all($param);		
		
		/*
		if ($query)
		{
			foreach ($query as $row)
			{
				echo $row->descripcion;
			}
		}else{
			echo 'nada';
		}*/
	
		return $query;
	}

	public function avisos() {
		$sedeId = $this->paramSede();
		$param = 'sede_id = '.$sedeId;
		//offset
		$uri_segment = 4;
		$offset = $this->uri->segment($uri_segment);
		
		$nameSede = $this->nameSede();
		//$query = $this->getAvisos();
		$query = $this->Model_Aviso->getPagedList($this->limit, $offset,$param);

		$data['contenido'] = 'home/aviso';
		$data['titulo'] = 'Avisos';
		$data['sede_name'] = $nameSede;
		$data['query'] = $query;
		$this->load->library('pagination');
		
		$config['base_url'] = base_url().'home/avisos/'.$sedeId;
		$config['total_rows'] = $this->Model_Aviso->count($param);
		$config['per_page'] = $this->limit;
		$config['uri_segment'] = $uri_segment;
		
		$this->pagination->initialize($config);
		$data['paginado'] = $this->pagination->create_links();

		$this->load->view('templateDefault', $data);
	}	

	public function actividades() {
		$sedeId = $this->paramSede();
		$param = 'sede_id = '.$sedeId;
		//offset
		$uri_segment = 4;
		$offset = $this->uri->segment($uri_segment);
				
		$nameSede = $this->nameSede();		
		$query = $this->Model_Actividad->getPagedList($this->limit, $offset,$param);
				
		$data['contenido'] = 'home/actividad';
		$data['titulo'] = 'Actividades';
		$data['sede_name'] = $nameSede;
		$data['query'] = $query;
		$this->load->library('pagination');
		
		$config['base_url'] = base_url().'home/actividades/'.$sedeId;
		$config['total_rows'] = $this->Model_Actividad->count($param);
		$config['per_page'] = $this->limit;
		$config['uri_segment'] = $uri_segment;
		
		$this->pagination->initialize($config);
		$data['paginado'] = $this->pagination->create_links();		
		
		$this->load->view('templateDefault', $data);
	}

	public function examenes() {
		$nameSede = $this->nameSede();
				
		$data['contenido'] = 'home/examen';
		$data['titulo'] = 'Fechas de Examenes';
		$data['sede_name'] = $nameSede;
		$data['subtitulo'] = 'Examenes';
		$this->load->view('templateDefault', $data);
	}

	public function pdi() {
		$nameSede = $this->nameSede();
		
		$data['contenido'] = 'home/pdi';
		$data['titulo'] = 'Puntos de Inter&eacute;s';
		$data['sede_name'] = $nameSede;
		$data['subtitulo'] = 'Puntos de Inter&eacute;s';
		$this->load->view('templateDefault', $data);
	}	
	
}
